package com.xjy.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.common.exception.UtilException;
import com.xjy.common.utils.PageUtils;
import com.xjy.system.domain.bo.SysContractEditBo;
import com.xjy.system.domain.bo.SysContractResultAddBo;
import com.xjy.system.domain.bo.SysContractResultEditBo;
import com.xjy.system.domain.bo.SysContractResultQueryBo;
import com.xjy.system.domain.vo.SysContractResultVo;
import com.xjy.system.domain.vo.SysContractVo;
import com.xjy.system.domain.vo.SysMaterialUpdateVo;
import com.xjy.system.domain.vo.SysPayaccountVo;
import com.xjy.system.service.ISysContractService;
import com.xjy.system.service.ISysContractTypeService;
import com.xjy.system.service.ISysPayaccountService;
import com.xjy.system.utils.BeanMapper;
import com.xjy.system.utils.CommonPage;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xjy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjy.system.domain.SysContractResult;
import com.xjy.system.mapper.SysContractResultMapper;
import com.xjy.system.service.ISysContractResultService;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 合同结算项目信息Service业务层处理
 *
 * @author xjy
 * @date 2022-02-08
 */
@Service
public class SysContractResultServiceImpl extends ServicePlusImpl<SysContractResultMapper, SysContractResult> implements ISysContractResultService {

	@Autowired
	private ISysContractTypeService iSysContractTypeService;
	@Autowired
	private ISysContractService iSysContractService;
	@Autowired
	private ISysPayaccountService iSysPayaccountService;


    @Override
    public SysContractResultVo queryById(Long id){
		SysContractResultVo rsp = null;

		SysContractResultQueryBo sysContractResultQueryBo = new SysContractResultQueryBo();
		sysContractResultQueryBo.setId(id);
		List<SysContractResultVo> list = baseMapper.queryPageList(sysContractResultQueryBo);
		if(null != list && list.size() > 0){
			rsp = list.get(0);
		}
        return rsp;
    }

    @Override
    public TableDataInfo<SysContractResultVo> queryPageList(SysContractResultQueryBo query) {
		List<SysContractResultVo> list = baseMapper.queryPageList(query);
		return PageUtils.buildDataInfo(list);
    }

	@Override
	public List<SysContractResultVo> selectList(SysContractResultQueryBo bo) {
		bo.setAuditStatus(true);
		List<SysContractResultVo> list = baseMapper.queryPageList(bo);
		return list;
	}

	@Override
    public List<SysContractResultVo> queryList(SysContractResultQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysContractResultVo.class);
    }

    private LambdaQueryWrapper<SysContractResult> buildQueryWrapper(SysContractResultQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysContractResult> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getProjName()), SysContractResult::getProjName, bo.getProjName());
        return lqw;
    }

	@Transactional
    @Override
    public Boolean insertByAddBo(SysContractResultAddBo bo) {
        SysContractResult add = BeanUtil.toBean(bo, SysContractResult.class);

        return save(add);
    }

	@Transactional
    @Override
    public Boolean updateByEditBo(SysContractResultEditBo bo) {
        SysContractResult update = BeanUtil.toBean(bo, SysContractResult.class);
        if(null == update.getProjAmt() || update.getProjAmt().compareTo(new BigDecimal(0))<1){
			throw new UtilException("结算金额必须大于0！");
		}
        return updateById(update);
    }


	@Transactional
	@Override
	public synchronized Boolean auditByEditBo(SysContractResultEditBo bo) {
		SysContractResultVo sysContractResultVo = this.queryById(bo.getId());
		if (sysContractResultVo.getAuditStatus()) {
			throw new UtilException("该数据无需重新审批！");
		}

		SysContractResult update = BeanUtil.toBean(bo, SysContractResult.class);
		update.setAuditStatus(true);
		if (null == update.getProjAmt() || update.getProjAmt().compareTo(new BigDecimal(0)) < 1) {
			throw new UtilException("结算金额必须大于0！");
		}
		//审核
		if (update.getAuditStatus()) {
			//更新合同结算额
			Long contractId = bo.getContractId();
			if(null == contractId){
				throw new UtilException("审核时,入参合同ID不能为空！");
			}
			Long colapyAccount = bo.getColapyAccount();
			if(null == colapyAccount){
				throw new UtilException("审核时,入参算账户ID不能为空！");
			}
			//当前合同信息
			SysContractVo sysContractVo =  iSysContractService.queryById(contractId);

			//更新金额
			SysContractEditBo sysContractEditBo = new SysContractEditBo();
			sysContractEditBo.setId(sysContractVo.getId());
			BigDecimal closeAmount = sysContractVo.getCloseAmount().add(bo.getProjAmt());
			if(closeAmount.compareTo(new BigDecimal(0)) < 1){
				throw new UtilException("合同结算金额不能小于0");
			}
			sysContractEditBo.setCloseAmount(closeAmount);
			iSysContractService.updateByEditBo(sysContractEditBo);

			//获取审批时的账号信息
			SysPayaccountVo sysPayaccountVo = iSysPayaccountService.queryById(colapyAccount);
			update.setFreeAmt(sysPayaccountVo.getAccountBalance());
		}
		return updateById(update);
	}

	/**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysContractResult entity){
        //TODO 做一些数据校验,如唯一约束
    }

	@Transactional
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }

		//更新合同结算额
		BigDecimal sub = new BigDecimal(0.0);
		for (Long id : ids) {
			SysContractResult scr = getVoById(id, SysContractResult.class);
			SysContractVo scVo = iSysContractService.queryById(scr.getContractId());
			SysContractEditBo scBo = BeanMapper.map(scVo,SysContractEditBo.class);

			scVo.setCloseAmount(scVo.getCloseAmount().subtract(scr.getProjAmt()));
			iSysContractService.updateByEditBo(scBo);
		}

        return removeByIds(ids);
    }

}
